// pages/vip/vip_index/vip_index.js
const db = wx.cloud.database()
Page({

    /**
     * 页面的初始数据
     */
    data: {

        user_url: 'cloud://peiyoukaoliantong-8e910u9ad85580.7065-peiyoukaoliantong-8e910u9ad85580-1317756011/静态资源/null_head.png',
        user_title: '昵称未授权',
        user_type: '',
        openId: '',
        user_vip: '',
        price: {},
        // 商品渲染数组
        body: [{
                id: 0,
                origin_price: '1000',
                price: '1000',
                desc: '',
                type: 'SVIP Pro',
                tag: 'hot',
                imageURL: '/svg/pro.svg',
            },
            {
                id: 1,
                origin_price: '1000',
                price: '1000',
                desc: '',
                type: 'SVIP',
                tag: '',
                imageURL: '/svg/svip.svg',
            },
            {
                id: 2,
                origin_price: '1000',
                price: '1000',
                desc: '',
                type: 'VIP',
                tag: '实惠',
                imageURL: '/svg/vip.svg',
            },
        ],
        option: [{
                text: '公共英语',
                value: 0
            },
            {
                text: '高等数学',
                value: 1
            },
            {
                text: '公共化学',
                value: 2
            },
            {
                text: '文学概论',
                value: 3
            },
            {
                text: '基础会计',
                value: 4
            },
            {
                text: '大学语文',
                value: 5
            },
            {
                text: '旅游学概论',
                value: 6
            },
            {
                text: '现代汉语',
                value: 7
            },
            {
                text: '马克思主义哲学',
                value: 8
            },
            {
                text: '地理学概论',
                value: 9
            },
            {
                text: '数据结构',
                value: 10
            },
            {
                text: '结构力学',
                value: 11
            },
            {
                text: '电路学',
                value: 12
            },
            {
                text: '植物学',
                value: 13
            },
            {
                text: '动物学',
                value: 14
            },
            {
                text: '中医综合',
                value: 15
            },
            {
                text: '医学综合',
                value: 16
            },
            {
                text: '护理综合',
                value: 17
            },
            {
                text: ' 机械设计基础',
                value: 18
            },
            {
                text: ' 水利学',
                value: 19
            },
            {
                text: ' 地质学概论',
                value: 20
            },
            {
                text: ' 物理',
                value: 21
            },
            {
                text: '专业化学',
                value: 22
            },
            {
                text: '英语综合阅读能力',
                value: 23
            },
            {
                text: '测绘学',
                value: 24
            },
            {
                text: '针灸推拿综合',
                value: 25
            },
            {
                text: '学前儿童心理与教育',
                value: 26
            },
        ],
        user_default: 0,
        // 表格
        listData: [{
                "code": "单科刷题",
                "text": "✓",
                "type": "✓",
                "big": "✓"
            },
            {
                "code": "单科考试",
                "text": "✓",
                "type": "✓",
                "big": "✓"
            },
            {
                "code": "专业查询",
                "text": "✓",
                "type": "✓",
                "big": "✓"
            },
            {
                "code": "历年真题",
                "text": "✕",
                "type": "✓",
                "big": "✓"
            },
            {
                "code": "备考资料",
                "text": "✕",
                "type": "✓",
                "big": "✓"
            },
            {
                "code": "教师答疑",
                "text": "✕",
                "type": "✓",
                "big": "✓"
            },
            {
                "code": "全程网课",
                "text": "✕",
                "type": "✕",
                "big": "✓"
            },


        ]
    },
    //改变使用科目
    onSwitch1Change(e) {
        console.log(e)
        this.setData({
            user_default: e.detail
        })
        // 获取openid
        let openId = wx.getStorageSync('openId')
        let res = db.collection("user").doc(openId).update({
            data: {
                user_default: e.detail
            }
        })

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // 获取openid
        let openId = wx.getStorageSync('openId')
        // console.log(openId)
        // 查询数据库中是否有用户数据
        let res = db.collection("user").doc(openId).get()
            .then(res => {
                console.log(res)
                this.setData({
                    user_url: res.data.user_url,
                    user_title: res.data.user_title,
                    user_type: res.data.user_type,
                    user_default: res.data.user_default,
                    user_vip: res.data.user_vip
                })
            })


        db.collection("price").get()
            .then(res => {
                console.log(res.data[0])
                this.setData({
                    'body[0].price': res.data[0].SVIPPro,
                    'body[1].price': res.data[0].SVIP,
                    'body[2].price': res.data[0].VIP,
                    'body[0].origin_price': res.data[0].SVIPProD,
                    'body[1].origin_price': res.data[0].SVIPD,
                    'body[2].origin_price': res.data[0].VIPD,
                    'body[0].desc': res.data[0].SVIPPro_text,
                    'body[1].desc': res.data[0].SVIP_text,
                    'body[2].desc': res.data[0].VIP_text,
                })
            })
    },





    //使用微信支付
    pay: function (e) {

        let user_default = this.data.user_default
        let user_vip = this.data.user_vip
        for (let index = 0; index < user_vip.length; index++) {
            // if (Number(user_vip[index].subject) == user_default && Number(user_vip[index].type <= e.target.id)) {
            if (Number(user_vip[index].subject) == user_default  ) {

                //  会员已开提示
                wx.showToast({
                    title: '升级联系管理员！',
                    icon: 'error',
                    duration: 2000, //持续的时间
                })
                var tag = false
            }
        }
        if (tag != false) {
            let index = e.target.id
            let body = this.data.body
            let option = this.data.option
            let user_default = this.data.user_default
            console.log(user_default)
            //创建订单号需要的时间戳
            var timestamp_begin = Date.parse(new Date());
            var time_show_begin = this.formatDate(timestamp_begin) //转换时间戳
            var timestamp = timestamp_begin / 1000;
            var timestamp_end = timestamp_begin + 31536000000;
            var time_show_end = this.formatDate(timestamp_end) //转换时间戳

            // 小程序代码
            console.log(body[index].price)
            wx.cloud.callFunction({
                name: 'pay',
                data: {
                    outTradeNo: timestamp + 'p' + timestamp + 'e' + timestamp,
                    totalFee: (body[index].price) * 100,
                    body: option[user_default].text + '-' + body[index].type
                },
                success: res => {
                    const payment = res.result.payment
                    console.log(res)
                    wx.requestPayment({
                        timeStamp: payment.timeStamp,
                        nonceStr: payment.nonceStr,
                        package: payment.package,
                        signType: payment.signType,
                        paySign: payment.paySign,
                        success(res) {
                            console.log('pay success', res)

                            // 获取openid
                            let openId = wx.getStorageSync('openId')
                            // console.log(openId)
                            // 查询数据库中是否有用户数据

                            const _ = db.command
                            db.collection("user").doc(openId).update({
                                data: {
                                    user_vip: _.push([{
                                        subject: user_default,
                                        type:Number(index),
                                        name: option[user_default].text + '-' + body[index].type,
                                        time1: time_show_begin,
                                        time2: time_show_end,
                                        time3: timestamp_begin,
                                        time4: timestamp_end,
                                        price: body[index].price
                                    }])
                                }
                            })
                            wx.switchTab({

                                url: '/pages/tabbar/index/index',
                                success: function (res) {
                                    this.onLoad();
                                }
                            })

                        },

                    })







                },
                fail(err) {
                    console.error('pay fail', err)
                }

            })


        }



    },
    open_buy() {

        wx.redirectTo({

            url: '/pages/vip/vip_log/vip_log',
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.onLoad()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    // 时间戳转换
    formatDate: function (inputTime) {
        var date = new Date(inputTime);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '年' + m + '月' + d + '日' + '  ' + h + ':' + minute + ':' + second;
    },
})